package com.jd.jdmall.utils;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;

/**
 * Created by asus on 2017/2/10.
 */

public class DrawableUtil {

    /**
     * 创建圆角矩形
     *
     * @param color  颜色
     * @param radius 圆角半径
     * @return
     */
    public static Drawable getDrawable(int color, int radius) {
        // 创建drawable对象
        GradientDrawable drawable = new GradientDrawable();
        // 设置图形shape
        drawable.setShape(GradientDrawable.RECTANGLE);
        // 设置圆角半径
        drawable.setCornerRadius(radius);
        // 设置颜色
        drawable.setColor(color);
        return drawable;
    }

    /**
     * 创建状态选择器
     * @param nomColor  正常颜色
     * @param pressColor  按下颜色
     * @param radius  圆角大小
     * @return
     */
    public static Drawable getStateListDrawable(int nomColor, int pressColor, int radius) {
        Drawable nomDrawable = getDrawable(nomColor, radius);
        Drawable pressDrawable = getDrawable(pressColor, radius);
        StateListDrawable stateDrawable = new StateListDrawable();
        stateDrawable.addState(new int[]{android.R.attr.state_pressed}, pressDrawable);
        stateDrawable.addState(new int[]{}, nomDrawable);
        return stateDrawable;
    }


}
